﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dto;
using dao;
using System.Text.RegularExpressions;
using common;
using System.Collections;
namespace GTVAccountingSystem.view.form
{
    public partial class frmCreateNewUser : Form
    {
        UsersDAO usersDAO = new UsersDAO();
        private String firstNameText = "";
        //private String lastNameText = "";
        private String userNameText = "";
        private String passwordText = "";
        private String confirmPassText = "";
        private String emailText = "";
        private String addressText = "";
        private String phone1Text = "";
        String error = "";
        int index = -1;
        public frmCreateNewUser()
        {
            index = Constant.LIST_OPEN.Count;
            InitializeComponent();
            createCompany();
            setLanguage();
        }
        /* Author : Jenny
         * Date : 10/4/2012
         * function setLanguage
         * set language
         * no return
         */
        public void createCompany()
        {
            CompanyDAO company = new CompanyDAO();
            DataTable dt = company.getAllCompany();
            cboCompany.DataSource = dt;
            cboCompany.DisplayMember = "CompanyName";
            cboCompany.ValueMember = "ID";
        }
        public void setLanguage()
        {
            lblUsername.Text = Constant.RM.GetString("lblUsername", Constant.CULTURE) + " : ";
            lblPassword.Text = Constant.RM.GetString("lblPassword", Constant.CULTURE) + " : ";
            lblConfirmPass.Text = Constant.RM.GetString("lblConfirmPassword", Constant.CULTURE) + " : ";
            lblFullName.Text = Constant.RM.GetString("lblFullName", Constant.CULTURE) + " : ";
            lblEmail.Text = Constant.RM.GetString("lblEmail", Constant.CULTURE) + " : ";
            lblPhone1.Text = Constant.RM.GetString("lblPhone", Constant.CULTURE) + " : ";
            lblCompanyName.Text = Constant.RM.GetString("lblCompanyName", Constant.CULTURE) + " : ";
            btnSave.Text = Constant.TAB + Constant.RM.GetString("btnSave", Constant.CULTURE);
            btnClear.Text = Constant.TAB + Constant.RM.GetString("btnClear", Constant.CULTURE);
            this.Text = Constant.RM.GetString("frmCreateUser", Constant.CULTURE);
        }
        /* Author : Jenny
         * Date : 3/4/2012
         * function btnSave_Click
         * save the data in form create user
         * no return
         */
        private void btnSave_Click(object sender, EventArgs e)
        {
            validateUser();
            if (error.Equals(""))
            {
                UserInformationsDTO userInformation = new UserInformationsDTO();
                userInformation.FirstName = firstNameText;
                //userInformation.LastName = lastNameText;
                userInformation.Phone1 = phone1Text;
                userInformation.Email = emailText;
                userInformation.CompanyID = cboCompany.SelectedValue.ToString();
                UserLoginDTO userLogin = new UserLoginDTO();
                userLogin.Username = userNameText;
                userLogin.Password = passwordText;
                if (usersDAO.insertUser(userInformation, userLogin, Constant.DR_ACTIONS_VARIABLE["varInsert"].ToString(), "record in table UserInformation and"))
                {
                    MessageBox.Show("Insert user succesful!!!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /* Author : Jenny
         * Date : 3/4/2012
         * function validateUser
         * validate data before save
         * return Boolean
         */ 
        public void validateUser()
        {
            error = "";
            disableErrorLable();
            firstNameText = txtFirstName.Text.Trim();
            //lastNameText = txtLastName.Text.Trim();
            emailText = txtEmail.Text.Trim();
            phone1Text = txtPhone1.Text.Trim();
            userNameText = txtUserName.Text.Trim();
            passwordText = txtPassword.Text.Trim();
            confirmPassText = txtConfirmPassword.Text.Trim();
            if (firstNameText.Equals(""))
            {
                txtFirstName.BackColor = Color.MistyRose;
                error += "Input First Name\n";
            }
            /*if (lastNameText.Equals(""))
            {
                txtLastName.BackColor = Color.MistyRose;
                error += "Input Last Name\n";
            }*/
            if (emailText.Equals(""))
            {
                txtEmail.BackColor = Color.MistyRose;
                error += "Input Email\n";
            }
            else
            {
                if (!usersDAO.checkEmail(emailText))
                {
                    txtEmail.BackColor = Color.MistyRose;
                    error += "Email is invalid\n";
                }
            }
            if (phone1Text.Equals(""))
            {
                txtPhone1.BackColor = Color.MistyRose;
                error += "Input Phone1\n";
            }
            else
            {
                if (!usersDAO.checkPhoneNumber(phone1Text))
                {
                    txtPhone1.BackColor = Color.MistyRose;
                    error += "Phone 1 must be number\n";
                }
                else if (!(8 <= phone1Text.Length && phone1Text.Length <= 15))
                {
                    txtPhone1.BackColor = Color.MistyRose;
                    error += "The length of phone is not valid ( 8 - 15 )\n";
                }
            }
            if (userNameText.Equals(""))
            {
                txtUserName.BackColor = Color.MistyRose;
                error += "Input User Name\n";
            }
            else
            {
                if (!usersDAO.checkUserNameExists(userNameText))
                {
                    txtUserName.BackColor = Color.MistyRose;
                    error += "User name has already exists\n";
                }
            }
            if (passwordText.Equals(""))
            {
                txtPassword.BackColor = Color.MistyRose;
                error += "Input Password\n";
            }
            else if(passwordText.Length < 8)
            {
                txtPassword.BackColor = Color.MistyRose;
                error += "Password must be 8 - 30 characters\n";
            }
            if (confirmPassText.Equals(""))
            {
                txtConfirmPassword.BackColor = Color.MistyRose;
                error += "Input Confirm password\n";
            }
            else
            {
                if (!confirmPassText.Equals(txtPassword.Text.Trim()))
                {
                    txtConfirmPassword.BackColor = Color.MistyRose;
                    error += "Confirm password is not matched\n";
                }
            }
        }
        /* Author : Jenny
          * Date : 3/4/2012
          * function disableErrorLable
          * hide the error lable when start
          * no return
          */
        public void disableErrorLable()
        {
            txtFirstName.BackColor = SystemColors.Window;
            //txtLastName.BackColor = SystemColors.Window;
            txtPhone1.BackColor = SystemColors.Window;
            txtEmail.BackColor = SystemColors.Window;
            txtPassword.BackColor = SystemColors.Window;
            txtConfirmPassword.BackColor = SystemColors.Window;
            txtUserName.BackColor = SystemColors.Window;
        }        
        /* Author : Jenny
          * Date : 3/4/2012
          * function txtPassword_TextChanged
          * call checkPassword function when text in password change 
          * no return
          */
        private void txtPassword_TextChanged(object sender, EventArgs e)
        {        
            if (txtPassword.Text.Length > 8)
            {
                String score = usersDAO.checkPassword(txtPassword.Text);
                if (score.Equals("1"))
                    lblPassword.Text = "Password : ( length 8 - 30 ) WEAK";
                else if(score.Equals("2"))
                    lblPassword.Text = "Password : ( length 8 - 30 ) MEDIUM";
                else
                    lblPassword.Text = "Password : ( length 8 - 30 ) STRONG";
            }
        }
        /* Author : Jenny
          * Date : 3/4/2012
          * function btnClear_Click
          * Clear all data in this form
          * no return
          */
        private void btnClear_Click(object sender, EventArgs e)
        {
            txtFirstName.Text = "";
            //txtLastName.Text = "";
            txtEmail.Text = "";
            txtPhone1.Text = "";
            txtUserName.Text = "";
            txtPassword.Text = "";
            txtConfirmPassword.Text = "";
            disableErrorLable();
        }

        private void frmCreateNewUser_FormClosing(object sender, FormClosingEventArgs e)
        {
            Constant.LIST_OPEN.RemoveAt(index);
        }
    }
}
